/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.jsfconsole.agent;

import javax.faces.context.FacesContext;


/**
 * Represent Instance of a single event in JSF Lifecycle 
 * @author devil
 */
public class TraceEvent {
	//Time event occurred at
	private long time;
	//Source argument
	private EventType eventType;
	//Source ID of faces Request
	private Integer sourceId;
	//Additional arguments collected for this event
	private Object[] arguments;
	//Request path that initated this TraceEvent
	private String requestPath;
	
	public TraceEvent(EventType event, Integer facesID, Object... arguments) {
		this.eventType=event;
		this.sourceId=facesID;
		this.arguments=new Object[arguments.length+1];
		//Argument 0  is always reserved as the current context
		FacesContext context=(FacesContext)FacesContext.getCurrentInstance();
		this.arguments[0]=context;
		
		this.requestPath=context.getExternalContext().getRequestContextPath()+context.getExternalContext().getRequestServletPath();
		for(int i=1;i<this.arguments.length;i++){
			this.arguments[i] = arguments[i-1];
		}

		this.time=System.currentTimeMillis();
	}

	public Object[] getArguments() {
		return arguments;
	}

	public Integer getSourceId() {
		return sourceId;
	}
	
	/**
	 * Start time of the event
	 * @return
	 */
	public long getTime() {
		return time;
	}


	public EventType getEventType() {
		return eventType;
	}

	public void setEventType(EventType eventType) {
		this.eventType = eventType;
	}


	public String getRequestPath() {
		return requestPath;
	}

	public void setRequestPath(String requestPath) {
		this.requestPath = requestPath;
	}
	
	
	@Override
	public String toString() {
		return "Event:"+eventType +" : "+arguments[1] +" :: "+requestPath;
	}
}
